package com.anywhere.freightman.system.controller;

import com.anywhere.freightman.common.ContextKeys;
import com.anywhere.freightman.common.annotation.Permission;
import com.anywhere.freightman.common.entity.ReturnResp;
import com.anywhere.freightman.common.entity.user.Account;
import com.anywhere.freightman.common.entity.user.RoleQueryEntity;
import com.anywhere.freightman.common.entity.user.request.RoleRequest;
import com.anywhere.freightman.common.enumeration.CommonStatus;
import com.anywhere.freightman.common.ex.CustomException;
import com.anywhere.freightman.common.request.PageRequest;
import com.anywhere.freightman.system.model.Resource;
import com.anywhere.freightman.system.model.Role;
import com.anywhere.freightman.system.service.IRoleService;
import com.anywhere.freightman.system.service.impl.resp.RoleResp;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Set;

@RestController
@RequestMapping("role")
@Api(description = "角色相关")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Permission(code = "SYSTEM_ROLE")
    @GetMapping("page")
    @ApiOperation("分页")
    public PageInfo<Role> getPageList(HttpServletRequest httpRequest,
                                      RoleQueryEntity qm) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        qm.setTenantId(account.getCompanyId());

        PageRequest<RoleQueryEntity> pageRequest = new PageRequest<>();
        BeanUtils.copyProperties(qm, pageRequest);
        pageRequest.setEntity(qm);
        return roleService.getPageList(pageRequest);
    }

    @PostMapping
    @Permission(code = "SYSTEM_ROLE")
    public Role insertData(HttpServletRequest httpRequest,
                           @RequestBody RoleRequest request) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);

        Role model = new Role();
        BeanUtils.copyProperties(request, model);
        model.setTenantId(account.getCompanyId());
        model.setCreateTime(new Date());
        model.setStatus(CommonStatus.ENABLE);
        model.setType(account.getType());
        roleService.insert(model, request.getResIds());
        return model;
    }

    @PutMapping
    @Permission(code = "SYSTEM_ROLE")
    public Role updateData(HttpServletRequest httpRequest,
                           @RequestBody RoleRequest request) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        Role model = roleService.getById(request.getId());
        if (model == null || !account.getCompanyId().equals(model.getTenantId())) {
            throw new CustomException("角色信息不存在");
        }
        model.setName(request.getName());
        model.setRemark(request.getRemark());
        model.setCode(request.getCode());
        roleService.update(model, request.getResIds());
        return model;
    }

    @DeleteMapping
    @ApiOperation("删除")
    @Permission(code = "SYSTEM_ROLE")
    public ReturnResp delete(HttpServletRequest httpRequest,
                             @RequestParam("id") long id) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        Role model = roleService.getById(id);
        if (model != null && account.getCompanyId().equals(model.getTenantId())) {
            roleService.delete(model);
            return ReturnResp.buildOk();
        } else {
            throw new CustomException("角色不存在");
        }
    }

    @GetMapping("{id}")
    @ApiOperation("详情")
    @Permission(code = "SYSTEM_ROLE")
    public ReturnResp getDetails(HttpServletRequest httpRequest,
                                 @PathVariable("id") long id) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        Role model = roleService.getById(id);
        RoleResp resp = new RoleResp();
        if (model == null || !account.getCompanyId().equals(model.getTenantId())) {
            throw new CustomException("信息不存在");
        }
        BeanUtils.copyProperties(model, resp);

        Set<Resource> resources = roleService.getResource(resp.getId());
        resp.setResources(Lists.newArrayList(resources));

        return ReturnResp.buildOk(resp);
    }
}
